<template>
  <div class="app-container">
    <el-form :inline="true" ref="refForm" :model="form">
      <el-form-item label="查询时间" prop="time">
        <el-date-picker
          v-model="form.time"
           type="datetimerange"
          align="right"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="['00:00:00', '23:59:59']"
          value-format="yyyy-MM-dd HH:mm:ss">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="用户名：" prop="proxy">
        <el-input v-model="form.proxy" type="String" @keyup.enter.native="submitFilterForm"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button class="filter-item" type="primary" icon="el-icon-search" @click="submitFilterForm">搜索</el-button>
        <el-button @click="selfResetForm('refForm')">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table
      border
      :data="tableData"
      v-loading="loading"
      element-loading-text="加载中..."
      show-summary
      :summary-method="getSummaries"
      class="el-table-align-center">
      <el-table-column label="领取奖励时间" prop="createTime" width="180" align="center">
        <template slot-scope="{row}">
          {{row.createTime | toTime}}
        </template>
      </el-table-column>
      <el-table-column label="活动奖金日期" prop="day" width="180" align="center">
      </el-table-column>
      <el-table-column prop="username" label="用户名" align="center"></el-table-column>
      <el-table-column prop="loss" label="负盈利" align="center"></el-table-column>
      <el-table-column prop="rescue" label="救援比例" align="center"></el-table-column>
      <el-table-column label="领取奖励金额" align="center">
        <template slot-scope="{row}">
          {{row.amount | currency}}
        </template>
      </el-table-column>
      <!--status     * 领奖状态：-1未达标，0已达标未领取，1已领取，2已过期-->
    </el-table>
    <div class="pagination-container">
      <el-pagination background @size-change="gridSizeChange" @current-change="gridCurrentChange"
                     :current-page="filters.page" :page-sizes="gridPageSize" :page-size="filters.size"
                     :layout="gridLayout" :total="tableData.total">
      </el-pagination>
    </div>
  </div>
</template>


<script>
  import {getTigerRescueList as listApi} from "@/services/api/activity";
  import {grid} from '@/build'

  export default {
    name: "tiger-rescue-list",
    mixins: [grid],
    data() {
      const time = [moment().startOf('isoWeek').format('YYYY-MM-DD 00:00:00'), moment().format('YYYY-MM-DD 23:59:59')]
      const startTime = time[0],
        endTime = time[1];

      return {
        tableData: [],
        form: {
          time,
          username: null,
        },
        filters: {
          size: 20,
          page: 1,
          startTime,
          endTime,
          username: null,
        },

      }
    },
    methods: {
      selfResetForm(formName){
        this.resetForm(formName);
        this.submitFilterForm();
      },
      submitFilterForm() {
        const {time, username} = this.form;
        let startTime = undefined, endTime = undefined;
        if (!_.isUndefined(time) && _.isArray(time)) {
          startTime = time[0];
          endTime = time[1];
        }
        this.filters = {...this.filters, username, startTime, endTime}
        this.gridSearch()
      },
      getSummaries(param) {
        const {columns, data} = param;
        const sums = [];

        columns.forEach((column, index) => {
          const values = data.map(item => Number(item[column.property]));
          if (index === 0) {
            sums[index] = '汇总';
            return;
          } else if ([3, 5].indexOf(index) !== -1 && !values.every(value => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr);
              if (!isNaN(value)) {
                return prev + curr;
              } else {
                return prev;
              }
            }, 0);
          } else {
            sums[index] = '';
          }
        });

        return sums;
      }
    },
    mounted() {
      //tab页记录访问日志
      this.$store.dispatch(types.SAVE_OPERATION_LOG,{content:_.getContent(this.$route,'活动查询',true)});
      this.bindGet(listApi, 'tableData', {
        action: 'getGrid',
        sendingData: 'filters',
        format: data => {
          let result = [];
          _.forIn(data && data.list, (value, key) => {
            result.push({...value})
          })
          return result;
        }
      });
      this.submitFilterForm();
    }

  }
</script>

<style scoped>

</style>
